﻿using System;
using System.Net;

namespace Sonce.Service
{
	public abstract class ServiceBase
	{
		protected abstract Uri ServiceUri
		{
			get;
		}

		protected virtual void Request(object data, Action<string, Exception> response)
		{
			if (this.ServiceUri == null)
			{
				throw new ArgumentNullException("ServiceUri");
			}

			if (data == null)
			{
				throw new ArgumentNullException("data");
			}

			if (response == null)
			{
				throw new ArgumentNullException("response");
			}

			string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

			WebClient client = new WebClient();
			client.UploadStringCompleted += (sender, e) =>
			{
				response(e.Result, e.Error);
			};
			client.UploadStringAsync(this.ServiceUri, "POST", json);
		}
	}
}
